package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.controller.debug.ServletDebugInfo;
import com.conversationboard.logger.Logger;
import com.conversationboard.model.NoSuchThreadException;
import com.conversationboard.model.Thread;
import com.conversationboard.model.User;
import com.conversationboard.security.EncryptedKey;
import com.conversationboard.security.EncryptionException;

@WebServlet(name = "AdminDeleteSelectedThreadsControllerServlet", urlPatterns = "/Admin/AdminDeleteSelectedThreadsControllerServlet")
public class AdminDeleteSelectedThreadsControllerServlet extends HttpServlet {

	private static final long serialVersionUID = -4329615451499288897L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		int boardId = Integer.parseInt(request.getParameter("boardId"));
		int threadId = Integer.parseInt(request.getParameter("threadId"));

		String[] messageIDs = request.getParameterValues("checkbox");

		/* Convert to list of integers */

		List<Integer> messageIDsToBeDeleted = new LinkedList<Integer>();

		for (String messageID : messageIDs) {
			int messageId = Integer.parseInt(messageID);
			messageIDsToBeDeleted.add(messageId);
		}

		try {

			/* Check you have appropriate authority */

			String loginId = request.getUserPrincipal().getName();
			User user = User.get(loginId);

			String key = request.getParameter("key");
			EncryptedKey encryptedKey = new EncryptedKey();

			if (!encryptedKey.isValid(key, user.getLoginId())) {
				Logger.log(new Date(), "Part 1 of error: " + this.getClass().getName(), user);
				String postRequest = ServletDebugInfo.getRawRequest(request);
				Logger.log(new Date(), "Part 2 of error:\n\n" + postRequest, user);
				return;
			}

			if (!user.isBoardAdmin(boardId)) {
				request.setAttribute("message", "You are not authorised to perform that action.");
				RequestDispatcher dispatcher = request.getRequestDispatcher("/Pages/messagepage.jsp");
				dispatcher.forward(request, response);
				return;
			}

			/* Perform a multiple delete */

			Thread thread = Thread.get(boardId, threadId);
			thread.deleteMessages(user, messageIDsToBeDeleted);

			/* Forward back to the page you came from */

			response.sendRedirect(request.getContextPath() + "/ThreadSelectedControllerServlet?boardid=" + boardId + "&threadid=" + threadId);

		} catch (NoSuchThreadException e) {
			throw new ServletException(e);
		} catch (EncryptionException e) {
			throw new ServletException(e);
		} catch (SQLException e) {
			throw new ServletException(e);
		}
	}

}
